package com.miyouke.chat.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.miyouke.chat.entity.AppControl;
import com.miyouke.chat.exception.GlobalException;
import com.miyouke.chat.exception.Result;
import com.miyouke.chat.service.AppControlService;
import com.miyouke.chat.vo.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chufangzheng
 */
@RestController
@RequestMapping("chat")
@Api("调用对话Controller")
@Slf4j
public class ApplicationController {


    @Autowired
    private AppControlService appControlService;


    @ApiOperation("应用列表")
    @GetMapping(path="/list")
    public ResponseData  list(){

        ResponseData responseData = new ResponseData();
        responseData.setCode(0);
        responseData.setMsg("成功");
        responseData.setData(appControlService.applist());
        responseData.setCount(100);
        return  responseData;
    }

    @ApiOperation("添加应用")
    @PostMapping(path="/add")
    public Result add(@RequestBody  AppControl appControl){

        if(appControl == null){
            throw new GlobalException("未获取到应用信息");
        }
        appControlService.save(appControl);
        return Result.success();
    }

    @ApiOperation("删除应用")
    @PostMapping(path="/delete")
    public Result delete(@RequestBody  List<Integer> ids){

        if(ids == null){
            throw new GlobalException("未获取到应用信息");
        }
        appControlService.removeByIds(ids);
        return Result.success();
    }

    @ApiOperation("启动服务")
    @PostMapping(path="/start")
    public Result startApp(@RequestBody  List<Integer> ids){

        if(CollectionUtil.isEmpty(ids)){
            throw new GlobalException("未选择要启动的任务！");
        }
        return Result.success(appControlService.startApp(ids));
    }

    @ApiOperation("停止服务")
    @PostMapping(path="/stop")
    @ResponseBody
    public Result stopApp(@RequestBody List<Integer> ids){
        if(CollectionUtil.isEmpty(ids)){
            throw new GlobalException("未选择要启动的任务！");
        }
        return Result.success(appControlService.stopApp(ids)) ;
    }

}
